如何创建包含可变内容的Magento翻译字符串?

时间:2015-11-02 13:32:07

标签: php magento internationalization

在Magento中,在模块中执行翻译涉及调用帮助程序并调用其翻译函数E.G。

public class AccountListInfoAdapter2 extends BaseAdapter {

    private Activity mContext;
    private ArrayList<ModelClass> accounts;
    private DisplayImageOptions options;
    private LayoutInflater inflater;
    private prevClass mainActivityFragment;
    private ViewHolder holder;

    public AccountListInfoAdapter2(Activity activity, ArrayList<ModelClass> mAccountListData, prevClass mainActivityFragment) {
        this.mContext = activity;
        this.accounts = mAccountListData;
        this.mainActivityFragment = mainActivityFragment;
        inflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getCount() {
        if (accounts == null) {
            return 0;
        } else
            return accounts.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;

                view = inflater.inflate(R.layout.account_info_row, parent, false);
                holder = new ViewHolder();
                assert view != null;
                setViews(view);
                view.setTag(holder);
                holder.emailAddress.setText(accounts.get(position).getEmail());
                holder.fullName.setText(accounts.get(position).getFullName());

            holder.fullName.setId(position);
            holder.emailAddress.setId(position);


            //Check if full name has been changed
            holder.fullName.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length() != count) {
                        if (!accounts.get(position).getFullName().equals(s.toString())) {
                            accounts.get(position).setFullName(s.toString());
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!accounts.get(position).getFullName().equals(s.toString())) {
                        accounts.get(position).setFullName(s.toString());
                    }
                }
            });


            //Check if email name has been changed
            holder.emailAddress.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(s.length() != count) {
                        if (!accounts.get(position).getEmail().equals(s.toString())){
                            accounts.get(position).getEmail(s.toString());
                        }
                    }
                }
                @Override
                public void afterTextChanged(Editable s) {
                    if (!accounts.get(position).getEmail().equals(s.toString())) {
                        accounts.get(position).getEmail(s.toString());
                    }
                }
            });

        return view;
    }


    public static class ViewHolder {
        TextView emailAddress;
        EditText fullName;
    }


    public void setViews(View view){
        holder.emailAddress = (EditText) view.findViewById(R.id.accountInfoProfileEmailAddress);
        holder.fullName = (EditText) view.findViewById(R.id.accountInfoFullName);
    }


}

但是,我没有在网上找到任何关于翻译包含变量的字符串的资源。根据过去的经验,我知道这通常是通过在字符串中使用令牌来处理的,并使用其他参数定义令牌替换。

例如,GNU gettext的手册建议使用格式字符串进行翻译:

Mage::helper("core")->__("This is a string to translate");

虽然Yii Framework有一个类似但略有不同的格式:

sprintf (gettext ("Hello %s!"), username ());

从Magento的核心文件快速看来,看起来Magento使用C风格的格式字符串,例如在Mage_Adminhtml_Block_Api_User_Edit :: getHeaderText中可以找到以下内容:

Yii::t("default", "Hello {username}!", array("username"=>username()));

但我想进一步确认或建议,因为在线文档很少。

1 个答案:

答案 0 :(得分:2)

Magento使用以下语法进行翻译:

Mage::helper('catalog')->__("This is %s text %s", "First String", "Second String");

正如Markus Harrison所建议的,我正在为格式字符串添加这些文档:

Formatting link 1

Formatting link 2 - wiki